package com.jusda.api.task;

import com.jusda.api.external.MessageClient;
import com.jusda.entity.UserScore;
import com.jusda.service.query.QueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author:y022 23/04/2022 01:15
 * @description:
 */
@Slf4j
@Service
public class TaskManager implements InitializingBean {
    @Autowired
    private QueryService queryService;

    @Autowired
    private MessageClient messageClient;

    /**
     * 提供调度任务入口，从数据库拉取需要清理的数据，投递到mq中，让下游服务处理。
     */
    public void pushUserScoreTask() {
        List<UserScore> userScoreList = queryService.queryUserScoreByLevel();
        if (CollectionUtils.isEmpty(userScoreList)) {
            log.warn("数据库没有需要清理的数据,调度任务不会执行！");
        } else {
            log.debug("开始执行调度任务...");
            messageClient.batchSendMsg(userScoreList);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("init!!!");
    }
}
